﻿using System;
using System.Collections.Generic;
using DTC.Models;
using System.Data;
using System.Text.RegularExpressions;
using System.Configuration;
using System.IO;

namespace DTC.BLL
{
    public class Test_DatasManager
    {
        public static int InsertTestData(Test_Data dt)
        {
            try
            {
                return DAL.Test_DatasService.InsertTestData(dt);
            }
            catch (Exception)
            {

                throw;
            }
        }
        public static int CreateDataBase()
        {
            string ConnectString = ConfigurationManager.AppSettings["SQLiteConnectionString"];
            Regex reg = new Regex("Data Source=(.*?);", RegexOptions.IgnoreCase);
            Match m = reg.Match(ConnectString);
            string dbName = m.Groups[1].Value;
            try
            {
                if (!File.Exists(dbName))
                {
                    DAL.Test_DatasService.CreateDataBase();
                    DAL.Test_DatasService.auto_vacuum();
                    DAL.Test_DatasService.Create_Index();
                    return DAL.Test_DatasService.CreateTriger_DEL_AF_8();
                }
                else
                {
                    return 1;
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 使用事务的方式插入数据
        /// </summary>
        /// <param name="dts">数据</param>
        /// <returns></returns>
         public static int InsertTestDatas(List<Test_Data> dts)
        {
            try
            {
                return DAL.Test_DatasService.InsertTestDatas(dts);
            }
            catch (Exception)
            {
                
                throw;
            }
        }
        public static int InsertTestData(TestSpan ts, string Model, string GUID, params string[] value2)
        {
            Test_Data dt = new Test_Data();
            dt.GUID = GUID;
            dt.Max = ts.最大值;
            dt.Min = ts.最小值;
            dt.Result = ts.测试结果;
            dt.Name = ts.测试项目;
            dt.Value1 = ts.测试值;
            dt.Value2 = string.Join(",", value2);
            dt.Model = Model;
            return InsertTestData(dt);
        }
        public static List<Test_Data> getDatabyName(string Name, string beginDate, string EndDate)
        {
            try
            {
                return DAL.Test_DatasService.getDatabyName(Name, beginDate, EndDate);
            }
            catch (Exception)
            {
                throw;
            }
        }
        static public DataTable GetTestDataTable1(string BeginTime, string EndTime)
        {
            try
            {
                return DAL.Test_DatasService.GetTestDataTable(BeginTime, EndTime);
            }
            catch (Exception)
            {
                throw;
            }
        }
        static public DataTable GetTestDataTable(string BeginTime, string EndTime)
        {
            try
            {
                DataTable dt = DAL.Test_DatasService.GetTestDataTable(BeginTime, EndTime);
                List<string> corNames = new List<string>();
                foreach (DataColumn cor in dt.Columns)
                {
                    if (cor.ColumnName.Contains("RESULT"))
                    {
                        corNames.Add(cor.ColumnName);
                    }
                }
                //  foreach (DataRow dr in dt.Rows)
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DataRow dr = dt.Rows[i];
                    bool bResult = true;
                    foreach (string corName in corNames)
                    {
                        if (dr[corName].ToString().ToUpper().Contains("PASS"))
                        {
                            bResult = true;
                        }
                        else if (dr[corName].ToString().ToUpper().Contains("FAIL"))
                        {
                            bResult = false;
                        }
                        else if (string.IsNullOrEmpty(dr[corName].ToString()))
                        {
                            if (bResult)
                            {
                                dt.Rows[i].Delete();
                                break;
                            }
                        }
                    }
                }
                dt.AcceptChanges();
                return dt;
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
